package ldkxingzhe.ldkxingzhedemo.service;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.support.v4.app.Fragment;

import ldkxingzhe.ldkxingzhedemo.utils.LogDemo;

/**
 * Created by ke on 15-9-13.
 */
public class MessengerClientFragment extends Fragment{
    private final String TAG = MessengerClientFragment.class.getSimpleName();

    private static MessengerClientFragment messengerClientFragment = null;
    private Context context;
    Messenger mService = null;

    boolean mIsBound;

    public static MessengerClientFragment newInstance(){
        if(messengerClientFragment == null){
            messengerClientFragment = new MessengerClientFragment();
            messengerClientFragment.setRetainInstance(true);
        }
        return messengerClientFragment;
    }
    public MessengerClientFragment(){
        if(messengerClientFragment != null){
            throw new IllegalArgumentException("You should call MessengerClientFragment's constructor directly.");
        }
    }

    // Handler for response messages from the service
    final int MSG_REGISTER_CLIENT = 0;
    final int MSG_REMOVE_CLIENT = 1;
    final int MSG_SET_SIMPLE_VALUE= 2;
    final int MSG_SET_COMPLEX_VALUE = 3;
    class InComingHandler extends Handler{
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case MSG_SET_SIMPLE_VALUE:
                    LogDemo.v(TAG,"receive message from service, and msg.arg1 = " + msg.arg1);
                    break;
                case MSG_SET_COMPLEX_VALUE:
                    break;
                default:
                    break;
            }
        }
    }
    // need a messenger to receive message from service
    private Messenger messenger = new Messenger(new InComingHandler());

    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            LogDemo.v(TAG,"onServiceConnected");
            mService = new Messenger(service);
            Message msg = Message.obtain(null,MSG_REGISTER_CLIENT);
            msg.replyTo = messenger;
            try {
                mService.send(msg);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            LogDemo.v(TAG,"onServiceDisconnected");
            mService = null;
        }
    };

    public void doBindService(){
        context = getActivity().getApplicationContext();
        if(mIsBound = context.bindService(
                new Intent(MessengerService.class.getName()),
                serviceConnection,Context.BIND_AUTO_CREATE)){

        }else {

        }
        LogDemo.v(TAG,"doBindService");
    }

    public void doUnbindService(){
        if(mService == null){
            return;
        }
        if(mIsBound){
            Message msg = Message.obtain(null,MSG_REMOVE_CLIENT);
            msg.replyTo = messenger;
            try {
                mService.send(msg);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
        LogDemo.v(TAG,"doUnbindService");
        context.unbindService(serviceConnection);
        mIsBound = false;
        mService = null;
    }

    public void doSendSimpleData(){
        if(mService == null){
            return;
        }
        LogDemo.v(TAG,"doSendSimpleData");
        Message msg = Message.obtain(null,MSG_SET_SIMPLE_VALUE,this.hashCode(),0);
        try {
            mService.send(msg);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}
